#pragma once
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <string.h>

namespace thread_clock
{
    class ThreadDate
    {
    public:
        std::string _name;
        pthread_mutex_t *_lock;
    public:
      ThreadDate(const std::string &name,pthread_mutex_t *lock):_name(name),_lock(lock)
      {
      }
      ~ThreadDate()
      {
      }
    };

    typedef void (*func_t)(ThreadDate *td);

    class Thread
    {
    private:
        std::string _name;
        func_t _func;
        pthread_t _tid;
        bool _isrunning;
        ThreadDate *_td;
    public:
        void Excute()
        {
            std::cout <<  _name <<"is running"  << std::endl;
            _isrunning=true;
            _func(_td);
            _isrunning=false;
        }
    public:
        Thread(const std::string &name,func_t func,ThreadDate *td):_name(name),_func(func),_td(td)
        {
            std::cout << "create " << name << " done" << std::endl;
        }

        static void *ThreadRounte(void *args)
        {
            Thread *self=static_cast<Thread*>(args);
            self->Excute();
            return nullptr;
        } 
        bool start()
        {
            int n=pthread_create(&_tid,nullptr,ThreadRounte,this);
            if(n!=0) return false;
            else return true;
           
        }
        std::string status()
        {
            if(_isrunning) return "is running";
            else return "sleep";
        }
        void stop()
        {
            if(_isrunning)
            {
                pthread_cancel(_tid);
                _isrunning=false;
                std::cout << _name << " stop" <<std::endl;
            }
        }

        void join()
        {
            pthread_join(_tid,nullptr);
            std::cout << _name << " join" << std::endl;
        }
        std::string Name()
        {
            return _name;
        }
        ~Thread()
        {

        }
    };
    
}